#define _CRT_SECURE_NO_WARNINGS 1


#include<stdio.h>
int Add(int x, int y)
{
	return x + y;
}

int Sub(int x, int y)
{
	return x - y;
}

int Mul(int x, int y)
{
	return x * y;
}

int Div(int x, int y)
{
	return x / y;
}
int main()
{
	int(*pf1)(int, int) = Add;  //pf1,pf2,pf3,pf4为函数指针变量
	int(*pf2)(int, int) = Sub;
	int(*pf3)(int, int) = Mul;
	int(*pf4)(int, int) = Div;
	int (*pf[4])(int, int) = { Add,Sub,Mul,Div};//pf[4]为函数指针数组，此数组每个元素类型为函数指针类型
	int i = 0;
	for (i = 0; i < 4; i++)
	{
		printf("%d ", pf[i](6, 2));
	}
	return 0;
}
//实现计算机
#include<stdio.h>
int Add(int x, int y)
{
	return x + y;
}

int Sub(int x, int y)
{
	return x - y;
}

int Mul(int x, int y)
{
	return x * y;
}

int Div(int x, int y)
{
	return x / y;
}
void Menu()
{
	printf("**********************\n");
	printf("****1.Add    2.Sub****\n");
	printf("****3.Mul    4.Div****\n");
	printf("****    0.Exit    ****\n");
	printf("**********************\n");
	printf("请选择：");
}
int main()
{
	int input = 0;
	int x = 0;
	int y = 0;
	int ret = 0;
	do
	{
		Menu();
		scanf("%d", &input);
		switch (input)                        //如果想加入其他的运算，&，|，>>,<<···，就会产生很多case语句，而这些case语句都非常相似，                                                                     
		{                                                                          //这样就显得代码重复、冗余了
		case 1:
			printf("请输入操作数："); 
			scanf("%d %d", &x, &y);
			ret = Add(x, y);
			printf("%d\n", ret);
			break;
		case 2:
			printf("请输入操作数：");
			scanf("%d %d", &x, &y);
			ret = Sub(x, y);
			printf("%d\n", ret);
			break;
		case 3:
			printf("请输入操作数：");
			scanf("%d %d", &x, &y);
			ret = Mul(x, y);
			printf("%d\n", ret);
			break;
		case 4:
			printf("请输入操作数：");
			scanf("%d %d", &x, &y);
			ret = Div(x, y);
			printf("%d\n", ret);
			break;
		case 0:
			printf("退出计算机\n");
			break;
		default:
			printf("选择错误，请重新选择\n");
			break;
		}
	} while (input);
	return 0;
}


//优化后
#include<stdio.h>
int Add(int x, int y)
{
	return x + y;
}

int Sub(int x, int y)
{
	return x - y;
}

int Mul(int x, int y)
{
	return x * y;
}

int Div(int x, int y)
{
	return x / y;
}
void Menu()
{
	printf("**********************\n");
	printf("****1.Add    2.Sub****\n");
    printf("****3.Mul    4.Div****\n");
	printf("****    0.Exit    ****\n");
    printf("**********************\n");
	printf("请选择：");
}
void Calc(int(*pf)(int, int))
{
	int x = 0;
	int y = 0;
	int ret = 0;
	printf("请输入操作数：");
	scanf("%d %d", &x, &y);
	ret = pf(x, y);
	printf("%d\n", ret);
}

int main()
{
	int input = 0;
	do
	{
		Menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			Calc(Add);
			break;                             
		case 2:
			Calc(Sub);                                 
			break;
		case 3:
			Calc(Mul);
			break;
		case 4:
			Calc(Div);
			break;
		case 0:
			printf("退出计算机\n");
			break;
		default:
			printf("选择错误，请重新选择\n");
			break;
		}
	} while (input);
	return 0;
}



#include<stdio.h>
int Add(int x, int y)
{
	return x + y;
}

int Sub(int x, int y)
{
	return x - y;
}

int Mul(int x, int y)
{
	return x * y;
}

int Div(int x, int y)
{
	return x / y;
}
void Menu()
{
	printf("**********************\n");
	printf("****1.Add    2.Sub****\n");
	printf("****3.Mul    4.Div****\n");
	printf("****    0.Exit    ****\n");
	printf("**********************\n");
	printf("请选择：");
}
int (*pf[5])(int, int) = { 0,Add,Sub,Mul,Div};
                       //  0, 1,  2,  3,  4
void Calc(int input)
{
	int x = 0;
	int y = 0;
	int ret = 0;
	printf("请输入操作数：");
	scanf("%d %d", &x, &y);
	ret = pf[input](x, y);
	printf("%d\n", ret);
}
int main()
{
	int input = 0;
	do
	{
		Menu();
		scanf("%d", &input);
		if (input >= 1 && input <= 4)
		{
			Calc(input);
		}
		else if (input == 0)
		{
			printf("退出计算机\n");
			break;
		}
		else
		{ 
			printf("输入错误，请重新选择\n");
		}
	} while (input);
	return 0;
}

